package com.bwie.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.service.AuthService;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.UserRequest;
import com.bwie.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class AuthController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private AuthService authService;

    @PostMapping("/sendCode")
    public Result sendCode(@RequestParam String phone){
        log.info("功能名称:发送验证码,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                phone);
        Result result = authService.sendCode(phone);
        log.info("功能名称:发送验证码,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/Login")
    public Result Login(@RequestBody UserRequest userRequest){
        log.info("功能名称:用户登录,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(userRequest));
        Result login = authService.Login(userRequest);
        log.info("功能名称:用户登录,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(login));
        return login;
    }

    @PostMapping("/userInfo")
    public Result userInfo(){
        log.info("功能名称:获取用户消息,请求URI:{},请求方式:{}",
                request.getRequestURI(),
                request.getMethod()
               );
        User user = authService.userInfo();
        log.info("功能名称:获取用户消息,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(user));
        return Result.success(user);
    }

    @PostMapping("/regist")
    public Result regist(@RequestBody UserRequest userRequest) {
        log.info("功能名称:账号注册,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(userRequest));
        Result regist = authService.regist(userRequest);
        log.info("功能名称:账号注册,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(regist));
        return regist;
    }

}
